<?php
CUserAuth::hasAccess(STATUS_MITARBEITER_SCHREIBEN);
/** costumer_delete.php
 * Project name: project_name
 * Created on: 28.10.2011
 * Author: Matias Thomsen
 * 
 * Kunden auswahl aus einer Tabelle, die nur Kunden aus der eigenen Geschäfts-
 * stelle auslistet. Nach auswahl wird der Kunde gelöscht bzw. löschung
 * vorgemerkt werden.
 */

if (isset($_POST['abort'])) {
        header("LOCATION: ?page=customer-view");
        die("Fatal error!");
}

//-----INCLUDES---------
    include_once 'class/customer/CCustomerMapper.php';
//-----INCLUDES END-----

//-----DATABASE CONNECTION-----
    $db = new CCustomerMapper($connArgs);
//-----DATABASE CONNECTION END-----
    
//-----VARS-------------
    $staff = $_SESSION['userID']; //employee logged in
    $gstelle = $_SESSION['gstelleId']; //employees' workstation
    $showCustomerData = false;
    $showCustomerTable = true;
    $errors = array();
//-----VARS END---------

    
if ( isset($_POST['kunden-nr']) ){
        $customer = $db->getSingleCustomer($_POST['kunden-nr'], 0);
        
        if ( $customer ){

                if (isset($_POST['abort'])){


                }elseif (isset($_POST['delete'])){
                        $delete = $db->deleteCustomer($customer);
                        if ( $delete == true ){
                            $db->deleteCustomer($customer, 1);
                            $message = "Der Kunde wurde gelöscht.";
                        }else{
                            $errors[] = $delete;
                            //TODO fehlerbehandlung mark
                        }

                }elseif (isset($_POST['mark'])){
                        $deleteMark = $db->markDeletion($customer, $staff, "noch kein Kommentar");
                        if ( $deleteMark == false ){
                            $message = "Der Kunde wurde zur Löschung vorgemerkt.";
                        }else{
                            $errors[] = $deleteMark;
                            //TODO fehlerbehandlung delete
                        }

                }else{
                    die("Fatal Error!");
                }
                
        }

    //display error or success message
    if ( count($errors) > 0 ){ ?>
            <!-- Error message box -->
            <div class="display-errors-label">
                    Aktion ohne Erfolg
            </div>
            <div class="display-errors-massage">
                    <ul>
                    <?php
                        foreach ( $errors as $error ){
                                echo "<li>".$error."</li>";
                        }
                    ?>
                    </ul>
            </div>
<?php
    }else{
            $showCustomerTable = false; //disable table with customers
            ?>
                    <div id="message-box">
                            <p><?= $message ?></p>
                    </div>
            <?php
    }
    
}else{
    $showCustomerData = true;
}
    
    
    
    
//---------------------------------------
//-----TABLE WITH SELECTED CUSTOMER------
//---------------------------------------
//TODO betreuer is not mandatory
    if ( isset($_GET['kunden-nr']) && $showCustomerData == true ){
            $customer = $db->getSingleCustomer($_GET['kunden-nr'], 0);
            
            if ( $customer != false ){
            ?>
                    <div class="form-costumer-create">
                        <div class="form-costumer-create-2cols">
                            <div class="show-customer-data-headline"><?php 
                                echo $customer->getAnrede()." ";
                                if ( $customer->getTitel() == "Prof_dr"){
                                    echo "Prof. Dr. ";
                                }  elseif ( $customer->getTitel() == "Dr" ) {
                                    echo "Dr. ";
                                }  elseif ( $customer->getTitel() == "Prof" ) {
                                    echo "Prof. ";
                                }
                                echo $customer->getName()." ";
                                echo $customer->getNachname();
                                if ( $customer->getGeburtsname() != "" ){
                                    echo " (geb.: ".$customer->getGeburtsname().")";
                                }
                                if ( $customer->getBearbeiterName() != "" ){
                                    echo " - Zuletzt bearbeitet von: ".$customer->getBearbeiterName();
                                }
                                if ( $customer->getFreigeberName() != "" ){
                                    echo " - Freigegeben von: ".$customer->getFreigeberName();
                                }

                          ?></div>
                            <!--####################################################################-->
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Geschäftsstelle</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getGstelleBezeichnung() ?></div>
                                <div class="clear"></div>
                            </div>
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Betreuer</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getBetreuerName() ?></div>
                                <div class="clear"></div>
                            </div>
                            <!--####################################################################-->
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Kunden Nummer</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getKundenNr() ?></div>
                                <div class="clear"></div>
                            </div>
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Status</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getStatus() ?></div>
                                <div class="clear"></div>
                            </div>
                            <!--####################################################################-->
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Straße, Hausnummer</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getStrasse_nr() ?></div>
                                <div class="clear"></div>
                            </div>
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Plz, Ort</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getPlz() ?>,&nbsp;<?= $customer->getOrt() ?></div>
                                <div class="clear"></div>
                            </div>
                            <!--####################################################################-->
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Wohnhaft seit</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getWohnhaftSeit() ?></div>
                                <div class="clear"></div>
                            </div>
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Wohnsituation</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getWohnsituation() ?></div>
                                <div class="clear"></div>
                            </div>
                            <!--####################################################################-->
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Geburtsdatum</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getGeburtsdatum() ?></div>
                                <div class="clear"></div>
                            </div>
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Familienstand</div>
                                <div class="form-customer-create-input"><?php
                                    if ( $customer->getFamilienstand() == "1"){
                                        echo "Ledig";
                                    }  elseif ( $customer->getFamilienstand() == "2" ) {
                                        echo "Verheiratet";
                                    }  elseif ( $customer->getFamilienstand() == "3" ) {
                                        echo "Geschieden";
                                    }  elseif ( $customer->getFamilienstand() == "4" ) {
                                        echo "Zusammenlebend";
                                    }  elseif ( $customer->getFamilienstand() == "5" ) {
                                        echo "Getrennt Lebend";
                                    }  elseif ( $customer->getFamilienstand() == "6" ) {
                                        echo "Verwitwet";
                                    }  else {
                                        echo "Unbekannt";
                                    }
                              ?></div>
                                <div class="clear"></div>
                            </div>
                            <!--####################################################################-->
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Staatsangehörigkeit</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getStaatsange() ?></div>
                                <div class="clear"></div>
                            </div>
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">E-Mail</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getEmail() ?></div>
                                <div class="clear"></div>
                            </div>
                            <!--####################################################################-->
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Telefonnummer 1</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getTelefon_1() ?></div>
                                <div class="clear"></div>
                            </div>
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Telefonnummer 2</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getTelefon_2() ?></div>
                                <div class="clear"></div>
                            </div>
                            <!--####################################################################-->
                            <div class="show-customer-data-headline">Zusatz angaben</div>
                            <!--####################################################################-->
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Monatseinkommen</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getMonatEinkommen() ?></div>
                                <div class="clear"></div>
                            </div>
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Extra Einkommen</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getExtraEinkommen() ?></div>
                                <div class="clear"></div>
                            </div>
                            <!--####################################################################-->
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Berufsgruppe</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getBerufsgruppe() ?></div>
                                <div class="clear"></div>
                            </div>
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Branchen</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getBranche() ?></div>
                                <div class="clear"></div>
                            </div>
                            <!--####################################################################-->
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Beruf</div>
                                <div class="form-customer-create-input">&nbsp;<?= $customer->getBeruf() ?></div>
                                <div class="clear"></div>
                            </div>
                            <div class="form-customer-create-input-wrap">
                                <div class="form-customer-create-label">Datenschutzbedingung</div>
                                <div class="form-customer-create-input"><?php
                                    if ( $customer->getDatenschutz() == "1"){
                                        echo "akzeptiert";
                                    }  else {
                                        echo "nicht akzeptiert";
                                    }
                              ?></div>
                                <div class="clear"></div>
                            </div>
                            <!--####################################################################-->

                            <form name="enable" method="POST" action="<?php echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']; ?>">
                                <!-- Form element wrap -->
                            <div class="form-customer-create-submit-wrap">
                                    <div class="form-customer-create-submit">
                                        <input type="hidden" name="kunden-nr" value="<?= $_GET['kunden-nr'] ?>" />
                                        <input class="form-customer-create-button" type="submit" name="abort" value="Abbrechen"/>
                                        <?php if ( $_GET['action'] == "mark" ){ //show only if its NOT marked ?>
                                        <input class="form-customer-create-button" type="submit" name="mark" value="L&ouml;schung vormerken"/>
                                        <?php }?>
                                        <?php if ( $_GET['action'] == "delete" ){ //show only if its already marked ?>
                                        <input class="form-customer-create-button" type="submit" name="delete" value="L&ouml;schen"/>
                                        <?php }?>
                                    </div>

                            <!-- Clear Div -->
                            <div class="clear"></div>
                            <!-- Clear Div End -->
                            </div>
                            <!-- Form element wrap end -->
                            </form>
                        </div>
                    </div>
    <?php
                } //if ( $customer != 0 ){ ENDS
        }//if ( isset($_GET['kunden-nr']) ){ ENDS
        
        
//---------------------------------------
//-----TABLE WITH SELECTED CUSTOMER END--
//---------------------------------------
    
//---------------------------------------
//-----TABLE WITH CUSTOMERS--------------
//---------------------------------------
if ( $showCustomerTable ){
        if ( isset ($_GET['action']) ){
            if ( $_GET['action'] == "mark" ){
                    $action = array("delete");
                    $customers = $db->getCustomers("delete", $staff, $gstelle);//get/select all active customers

                    include_once("customer_table.php");
                
            }elseif ( $_GET['action'] == "delete" ){
                    $action = array("delete");
                    $customers = $db->getCustomers("marked-delete", $staff, $gstelle);//get/select all active customers

                    include_once("customer_table.php");
            }
            
        }
        
}
//---------------------------------------
//-----TABLE WITH CUSTOMERS END----------
//---------------------------------------

?>